<template>
  <div class="p-2" style="display: flex; flex-direction: column; height: 100%">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="巡视名称" prop="patrolTaskName">
              <el-input v-model="queryParams.patrolTaskName" placeholder="请输入巡视任务id" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="创建时间" prop="">
              <el-date-picker v-model="queryParams.startAndEnd" type="daterange" start-placeholder="开始日期" end-placeholder="结束日期" value-format="YYYY-MM-DD" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never" style="flex: 1; display: flex; flex-direction: column; position: relative">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['camera:patrolTaskInfoLog:remove']" type="danger" plainicon="Delete" :disabled="multiple" @click="handleDelete()" >
              删除
            </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="patrolTaskLogList" style="flex: 1;" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="任务执行编号" align="center" prop="cameraPatrolTaskExecutorCode" min-width="226" show-overflow-tooltip />
        <el-table-column label="巡视任务" align="center" prop="patrolTaskName" min-width="120" show-overflow-tooltip/>
        <el-table-column label="巡视路线" align="center" prop="patrolRouteName" min-width="120" show-overflow-tooltip/>
        <el-table-column label="任务时间" align="center" min-width="210" show-overflow-tooltip>
          <template #default="scope">
            {{ scope.row.taskStartTime + ' ~ ' + scope.row.taskEndTime }}
          </template>
        </el-table-column>
        <el-table-column label="任务创建人" align="center" prop="createName" min-width="120" show-overflow-tooltip/>
        <el-table-column label="任务时长" align="center" prop="taskDuration" show-overflow-tooltip/>
        <el-table-column label="巡视时长" align="center" prop="patrolDuration" show-overflow-tooltip/>
        <el-table-column label="事件上报次数" align="center" prop="eventNum" width="120" show-overflow-tooltip/>
        <el-table-column label="打卡时间" align="center" prop="clockTime" min-width="170" show-overflow-tooltip/>
        <el-table-column label="任务执行人" align="center" prop="executeUserName" min-width="120" show-overflow-tooltip/>
        <el-table-column label="执行时间" align="center" prop="startTime" min-width="170" show-overflow-tooltip />
        <el-table-column label="操作" align="center" fixed="right" width="110">
          <template #default="scope">
            <el-button link type="primary" @click="openDetail(scope.row)">详情</el-button>
            <el-button v-hasPermi="['camera:patrolTaskInfoLog:remove']" link type="primary" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <el-drawer v-model="dialog.visible" :title="dialog.title" size="800px" append-to-body destroy-on-close>
      <detail :record-id="selectedId" @success="getList" @close="dialog.visible = false" />
    </el-drawer>
  </div>
</template>

<script setup  lang="ts">
import { delpatroltaskinfolog, listpatroltaskinfolog } from '@/api/security/video-patrol/camera-patrol-task-info';
import { patroltaskinfologVO } from '@/api/security/video-patrol/camera-patrol-task-info/types';
import Detail from '@/views/security/video-patrol/camera-patrol-task-log/components/detail.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const patrolTaskLogList = ref<any[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const selectedId = ref();

const queryFormRef = ref<ElFormInstance>();
const patrolTaskLogFormRef = ref<ElFormInstance>();

const dialog = reactive<any>({
  visible: false,
  title: ''
});

const queryParams = ref<any>({
  pageNum: 1,
  pageSize: 10,
  patrolTaskId: undefined,
  executeUserId: undefined,
  patrolDuration: undefined,
  startTime: undefined,
  endTime: undefined,
  eventNum: undefined,
  params: {}
})

/** 查询巡视任务-巡视记录基本信息列表 */
const getList = async () => {
  loading.value = true;
  queryParams.value.startTime = queryParams.value.startAndEnd?.length ? queryParams.value.startAndEnd[0] + ' 00:00:00' : null;
  queryParams.value.endTime = queryParams.value.startAndEnd?.length ? queryParams.value.startAndEnd[1] + ' 23:59:59' : null;
  const res = await listpatroltaskinfolog(queryParams.value).finally(()=> { loading.value = false; })
  console.log('获取巡视记录：', res);

  patrolTaskLogList.value = res.rows;
  total.value = res.total;
};

/** 表单重置 */
const reset = () => {
  patrolTaskLogFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryParams.value = { pageNum: 1, pageSize: queryParams.value.pageSize }
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: patroltaskinfologVO[]) => {
  ids.value = selection.map((item) => item.cameraPatrolTaskInfoLogId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 打开详情弹窗 */
const openDetail = async (row?: patroltaskinfologVO) => {
  selectedId.value = row?.cameraPatrolTaskInfoLogId || ids.value[0];
  dialog.visible = true;
  dialog.title = '';
};

/** 删除按钮操作 */
const handleDelete = async (row?: patroltaskinfologVO) => {
  const _cameraPatrolTaskInfoLogIds = row?.cameraPatrolTaskInfoLogId || ids.value;
  await proxy?.$modal.confirm('是否确认删除巡视任务-巡视记录基本信息编号为"' + _cameraPatrolTaskInfoLogIds + '"的数据项？').finally(() => (loading.value = false));
  proxy.$modal.loading('正在删除，请稍候');
  await delpatroltaskinfolog(_cameraPatrolTaskInfoLogIds).finally(() => proxy.$modal.closeLoading())
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'camera/patrolTaskInfoLog/export',
    {
      ...queryParams.value
    },
    `patrolTaskInfoLog_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
<style lang="scss" scoped>
:deep(.el-card__header) {
  height: 60px;
}

:deep(.el-card__body) {
  height: calc(100% - 60px);
  display: flex;
  flex-direction: column;
}

:deep(.pagination-container) {
  margin-top: 0;
}
</style>
